//
//  ProductListDataModel.m
//  Castlery
//
//  Created by xieweizhi on 8/9/17.
//  Copyright © 2017 weizhi. All rights reserved.
//

#import "ProductListDataModel.h"
#import "APIClient.h"
#import <YYKit/YYKit.h>

@implementation ProductListDataModel

- (void)requestProductListWithPage:(NSInteger)page
                        completion:(void (^)(NSError *error, NSArray <Product *> *productList))completion {
    _currentPage = page;
    
    NSDictionary *params = @{@"page": @(page),
                             @"per_page": @(10)};
    
    [APIClient requestProductListWithParams:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if (completion) {
            NSArray *productList = [NSArray modelArrayWithClass:[Product class] json:responseObject];
            completion(nil, productList);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (completion) {
            completion(error, nil);
        }
    }];
}

@end
